#include "key.h"


typedef enum
{
    KEY_EVENT_NONE = 0,
    KEY_EVENT_FAIL_EDGE = 1,
    KEY_EVENT_DEBOUNCE_CHECK = 2,
    KEY_EVENT_PRESS = 3,

}KEY_EVENT_t;

static KEY_EVENT_t ePA0Event = KEY_EVENT_NONE;
static uint8_t u8KeyCnt = 0;

void key_init(void)
{
    ePA0Event = KEY_EVENT_NONE;
    // GPIO_InitTypeDef GPIO_InitStruct = {0};

    // /* GPIO Ports Clock Enable */
    // __HAL_RCC_GPIOA_CLK_ENABLE();

    // /*Configure GPIO pin : PA0 */
    // GPIO_InitStruct.Pin = GPIO_PIN_0;
    // GPIO_InitStruct.Mode = GPIO_MODE_EVT_FALLING;
    // GPIO_InitStruct.Pull = GPIO_PULLUP;
    // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void key_event_callback(void){
    ePA0Event = KEY_EVENT_FAIL_EDGE;
}


uint8_t key_task(void){
    switch(ePA0Event){
        case KEY_EVENT_FAIL_EDGE:
            ePA0Event = KEY_EVENT_DEBOUNCE_CHECK;
        case KEY_EVENT_DEBOUNCE_CHECK:
            HAL_Delay(50);
            if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET){   
                ePA0Event = KEY_EVENT_PRESS;
            }else{
                ePA0Event = KEY_EVENT_NONE;
                break;
            }     
        case KEY_EVENT_PRESS:
            u8KeyCnt = (u8KeyCnt + 1) % 2;  //0 close 1 open
            ePA0Event = KEY_EVENT_NONE;
            break;
        default:
            break;
    }
    return u8KeyCnt;
}
